package com.thon.controller.rest;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.thon.dao.util.Page;
import com.thon.entity.project.Preferential;
import com.thon.entity.user.Coupon;
import com.thon.service.project.PreferentialService;


@Controller
@RequestMapping(value="/api/preferential")
public class PreferentialRestController {

	@Autowired
	private PreferentialService preferentialService;
	@RequestMapping(value="/add",method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> addPreferential(@RequestBody Preferential preferential
			){		
			
			if(preferentialService.addPreferential(preferential))
				return new ResponseEntity(true,HttpStatus.OK);	
			else
				return new ResponseEntity(false,HttpStatus.OK);	
		}
	
	@RequestMapping(value="/update",method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> updatePreferential(@RequestBody Preferential preferential
			){		
		
			if(preferentialService.updatePreferential(preferential))
				return new ResponseEntity(true,HttpStatus.OK);
			else
				return new ResponseEntity(false,HttpStatus.OK);	
		}
	
	@RequestMapping(value="/listpage",method = RequestMethod.GET)
	public ResponseEntity<HttpEntity> listPagePreferential(	@RequestParam(value = "status", defaultValue = "-1") int status,
			@RequestParam(value = "projectCategoryId",defaultValue="0") int projectCategoryId,
			@RequestParam(value = "userId", defaultValue = "0") int userId,		
			@RequestParam(value = "tags", defaultValue = "") String tags,		
			@RequestParam(value = "s",defaultValue = "10")int pageSize,
			@RequestParam(value = "p",defaultValue = "1")int pageNo){		
		Page<Preferential> page = new Page<Preferential>();	
		page = preferentialService.listPreferential(pageNo,pageSize,status,projectCategoryId,userId,tags);
		if(page==null)
			return new ResponseEntity(false, HttpStatus.OK);
		else
			return new ResponseEntity(page, HttpStatus.OK);
		
		}
	
	@RequestMapping(value="/get",method = RequestMethod.GET)
	public ResponseEntity<HttpEntity> listPagePreferential(	@RequestParam(value = "id", required = true) int id)
			{		
		Preferential preferential = preferentialService.getPreferential(id);	
		if(preferential==null)
			return new ResponseEntity(false, HttpStatus.OK);
		else
			return new ResponseEntity(preferential, HttpStatus.OK);
		
		}
	
	@RequestMapping(value="/edit",method = RequestMethod.GET)
	public String editPagePreferential(@RequestParam(value = "id", required = true) int id,Model model)
			{		
		Preferential preferential = preferentialService.getPreferential(id);	
		if(preferential==null)
		{
			model.addAttribute("preferential", preferential);
		}
		
			return "preferential-edit";
			}
		
		
	
}
